package com.witmore.neutron.ai.web.apiv2.nls.config;

import com.alibaba.nls.client.protocol.NlsClient;
import com.witmore.neutron.ai.web.apiv2.nls.NlsTokenCache;
import com.witmore.neutron.ai.web.apiv2.nls.SpeechRecognizerClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description: NlsConfig
 * Author: 豆子高
 * Date: 2025/6/10 14:06
 */
@Configuration
public class NlsConfig {
    @Autowired
    private NlsProperties nlsProperties;

    @Autowired
    private NlsTokenCache nlsTokenCache;

    @Bean
    public NlsClient nlsClient() {
        String token = nlsTokenCache.getToken();
        //创建NlsClient实例,应用全局创建一个即可,生命周期可和整个应用保持一致,默认服务地址为阿里云线上服务地址
        if (nlsProperties.getEndpoint().isEmpty()) {
            return new NlsClient(token);
        } else {
            return new NlsClient(nlsProperties.getEndpoint(), token);
        }
    }

    @Bean
    public SpeechRecognizerClient speechRecognizerClient(NlsClient nlsClient) {
        return new SpeechRecognizerClient(nlsClient, nlsProperties);
    }

}
